package com.ld.security.shop.crawler.modules.wo.service.impl;

import cn.hutool.http.HttpUtil;
import com.ld.security.common.core.base.column.MapJson;
import com.ld.security.common.core.util.FileUtil;
import com.ld.security.common.core.util.UuidUtil;
import com.ld.security.shop.crawler.modules.wo.entity.WoTask;
import com.ld.security.shop.crawler.modules.wo.service.IWoFileService;
import com.ld.security.shop.crawler.modules.wo.entity.WoFile;
import com.ld.security.common.mybatis.base.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;

@Service
@Transactional
class WoFileServiceImpl extends ISuperServiceImpl<WoFile> implements
        IWoFileService {

    String rootPath = "F:/fip/";


    @Override
    public synchronized WoFile downFile(WoTask woTask , String url) {
        String id = UuidUtil.getId(url);
        WoFile e = this.getById(id);
        if (e == null) {
            String fileName = id + FileUtil.getSuffix(url);
            File f = new File(rootPath + fileName);
            HttpUtil.downloadFile(url, f);
            e =  this.newInstance();
            e.setId(id);
            e.setFileUrl(f.getPath());
            MapJson mapJson =  new MapJson();
            mapJson.put("taskId",woTask.getLabel());
            e.setMapJson(mapJson);
            e.setTaskId(woTask.getId());
            e.setFileType(FileUtil.getSuffix(url));

            this.save(e);
        }
        return e;
    }
}
